package com.zjitc.serviceedu.utils;

import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 配置类，用于存储支付宝相关配置。
 */
@Getter
@NoArgsConstructor
@Component
public class AliPayConfig implements InitializingBean {

    // 应用ID，收款账号即为对应支付宝账号
    @Value("${alipay.appId}")
    private String appId;

    // 应用私钥，工具生成的私钥
    @Value("${alipay.merchantPrivateKey}")
    private String merchantPrivateKey;

    // 支付宝公钥，APPID下的支付宝公钥
    @Value("${alipay.alipayPublicKey}")
    private String alipayPublicKey;

    // 支付成功后的通知地址
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    @Override
    public void afterPropertiesSet() {
        // 确保所有配置项都已正确加载
        validateConfig();
    }

    private void validateConfig() {
        if (appId == null || appId.isEmpty()) {
            throw new IllegalArgumentException("支付宝 appId 不能为空");
        }
        if (merchantPrivateKey == null || merchantPrivateKey.isEmpty()) {
            throw new IllegalArgumentException("支付宝商户私钥不能为空");
        }
        if (alipayPublicKey == null || alipayPublicKey.isEmpty()) {
            throw new IllegalArgumentException("支付宝公钥不能为空");
        }
        if (notifyUrl == null || notifyUrl.isEmpty()) {
            throw new IllegalArgumentException("支付宝通知地址不能为空");
        }
    }
}
